home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / processes / mpdelayuntiltest / mpdelayuntiltest.c next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.7 KB  |  165 lines

  1. /*
  2.     File:        "MPDelayUntilTest.c"
  3.     
  4.     Description:    This is a application to test/demo the MT/MP MPDelayUntil API.
  5.  
  6.     Version:    v0.0
  7.  
  8.     File Ownership:
  9.  
  10.         Technology:            MultiTasking/MultiProcessing
  11.  
  12.     Copyright:     © Copyright 2000 Apple Computer, Inc. All rights reserved.
  13.     
  14.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  15.                 ("Apple") in consideration of your agreement to the following terms, and your
  16.                 use, installation, modification or redistribution of this Apple software
  17.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  18.                 please do not use, install, modify or redistribute this Apple software.
  19.  
  20.                 In consideration of your agreement to abide by the following terms, and subject
  21.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  22.                 copyrights in this original Apple software (the "Apple Software"), to use,
  23.                 reproduce, modify and redistribute the Apple Software, with or without
  24.                 modifications, in source and/or binary forms; provided that if you redistribute
  25.                 the Apple Software in its entirety and without modifications, you must retain
  26.                 this notice and the following text and disclaimers in all such redistributions of
  27.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  28.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  29.                 Apple Software without specific prior written permission from Apple.  Except as
  30.                 expressly stated in this notice, no other rights or licenses, express or implied,
  31.                 are granted by Apple herein, including but not limited to any patent rights that
  32.                 may be infringed by your derivative works or by other works in which the Apple
  33.                 Software may be incorporated.
  34.  
  35.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  36.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  37.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  39.                 COMBINATION WITH YOUR PRODUCTS.
  40.  
  41.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  42.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  43.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  45.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  46.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  47.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  48.  
  49.  
  50.     Author Initials:
  51.  
  52.     gaw
  53.  
  54. */
  55.  
  56. #pragma mark #Includes
  57. #include <MacTypes.h>
  58. #include <Fonts.h>
  59. #include <Multiprocessing.h>
  60.  
  61. #include <stdio.h>            // for printf & fflush
  62. #include <SIOUX.h>            // for SIOUXSettings stuff
  63. #include <SIOUXGlobals.h>    // for SIOUXQuitting
  64.  
  65. #pragma mark typedefs, structs, enums, defines, etc.
  66. typedef struct MyTaskParam_Struct
  67. {
  68.     Duration    fDuration;
  69.     OSStatus    fOSStatus;
  70.     Boolean*    fFlagPtr;
  71. } MyTaskParam_Rec,*MyTaskParam_Ptr,**MyTaskParam_Hdl;
  72.  
  73. #pragma mark static (local) functions
  74. // This is our MP task
  75. static OSStatus MyTask(void *parameter)
  76. {
  77.     // convert our (void*) parameter into a pointer to our task parameter record
  78.     MyTaskParam_Ptr tMyTaskParam_Ptr = (MyTaskParam_Ptr) parameter;
  79.     AbsoluteTime expirationTime;
  80.  
  81.     // calculate the expiration time based on the current
  82.     // absolute time plus a duration and arm the timer.
  83.     expirationTime = UpTime();
  84.     expirationTime = AddDurationToAbsolute(tMyTaskParam_Ptr->fDuration,expirationTime);
  85.  
  86.     // Ok, Delay until our expiration time.
  87.     tMyTaskParam_Ptr->fOSStatus = MPDelayUntil(&expirationTime);
  88.  
  89.     // set our done flag
  90.     *tMyTaskParam_Ptr->fFlagPtr = true;
  91.  
  92.     // and return our status.
  93.     return (tMyTaskParam_Ptr->fOSStatus);
  94. }
  95.  
  96. #pragma mark exported functions
  97. void main(void)
  98. {
  99.     MyTaskParam_Rec tMyTaskParam_Rec;
  100.     OSStatus        anErr;
  101.     MPTaskID        tMyMPTaskID;
  102.     UInt32            i, num;
  103.     volatile Boolean            done = false;
  104.     char            buff[10];    
  105.  
  106.     // Set the SIOUX window defaults
  107.     SIOUXSettings.autocloseonquit    = false;
  108.     SIOUXSettings.asktosaveonclose    = false;
  109.     SIOUXSettings.showstatusline    = false;
  110.     SIOUXSettings.columns            = 132;
  111.     SIOUXSettings.rows                = 24;
  112.     SIOUXSettings.fontsize            = 10;
  113.     GetFNum("\pMonaco",&SIOUXSettings.fontid);
  114.     SIOUXSettings.standalone        = true;
  115.  
  116.     printf("MPDelayUntilTest starting!\n");
  117.  
  118.     // Make sure that the MP library is loaded
  119.     if    (!MPLibraryIsLoaded())
  120.     {
  121.         printf("The MP library did not load.\n");
  122.         return;
  123.     }
  124.  
  125.     {    // for giggles we'll dump out the MP Library version info
  126.         Ptr tVersionCStringPtr;
  127.         UInt32 major,minor,release,revision;
  128.  
  129.         _MPLibraryVersion(&tVersionCStringPtr,&major,&minor,&release,&revision);
  130.  
  131.         printf("\n Version: \"%s\", major: %ld, minor: %ld, release: %ld, revision: %ld.\n",tVersionCStringPtr,major,minor,release,revision);
  132.     }
  133.  
  134.     // setup our task parameters
  135.     tMyTaskParam_Rec.fDuration = 10 * durationSecond;
  136.     tMyTaskParam_Rec.fOSStatus = noErr;
  137.     tMyTaskParam_Rec.fFlagPtr = (Boolean*) &done;
  138.  
  139.     // create our task
  140.     anErr = MPCreateTask(MyTask, (void *) &tMyTaskParam_Rec,0,NULL,0,0,kNilOptions,&tMyMPTaskID);
  141.     if (noErr != anErr)
  142.         printf(" MPCreateTask: %d.\n", anErr);
  143.     else
  144.         printf("Waiting 10 seconds...\n");
  145.  
  146.     // it's importaint that our WaitNextEvent loop not be CPU bound
  147.     // so we use a sleep time of one with WNE.
  148.     while (!done && (noErr == anErr) && !SIOUXQuitting)
  149.     {
  150.         EventRecord theEvent;
  151.  
  152.         WaitNextEvent(everyEvent,&theEvent,1,nil);
  153.         SIOUXHandleOneEvent(&theEvent);    // tell SIOUX to handle the event
  154.     }
  155.  
  156.     // if our task had an error then report it
  157.     if (noErr != tMyTaskParam_Rec.fOSStatus)
  158.         printf("MyTask error: %d.",tMyTaskParam_Rec.fOSStatus);
  159.  
  160.     if (noErr == anErr)
  161.         printf("Done!\n");
  162.  
  163.     printf("Press command-Q to quit.");
  164. }
  165.